package com.youlu.campus.admin.user.controller;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.UserBlackList;
import com.youlu.campus.entity.VO.req.UserBlackListQueryVO;
import com.youlu.campus.service.visitor.UserBlackListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Objects;

@Slf4j
@RestController
public class UserBlackListController {
    @Autowired
    private UserBlackListService userBlackListService;

    @PostMapping("/user-black-list/list")
    public RestResult list(@RequestBody @Valid UserBlackListQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<UserBlackList> datas = userBlackListService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/user-black-list/create")
    public RestResult create(@RequestBody UserBlackList req) {
        String comId = AuthUtils.getComId();
        req.setOptUser(AuthUtils.getLoginName());
        // 单次 添加
        req.setOptWay("1");
        boolean re = userBlackListService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/user-black-list/update")
    public RestResult list(@RequestBody @Valid UserBlackList req) {
        String comId = AuthUtils.getComId();
        boolean re = userBlackListService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/user-black-list/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        UserBlackList re = userBlackListService.findOne(id);
        if (Objects.nonNull(re)) {
            return RestResultBuilder.builder().success(re).build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/user-black-list/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = userBlackListService.delete(id);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/user-black-list/batch/create")
    public RestResult create(@RequestParam("comments") String comments,
                             @RequestParam("file") MultipartFile file) {
        String comId = AuthUtils.getComId();
        try {
            boolean re = userBlackListService.create(AuthUtils.getLoginName(), file.getBytes(), file.getName(), comments);
            if (re) {
                return RestResultBuilder.builder().success().build();
            }
            return RestResultBuilder.builder().failure().build();
        } catch (BusinessException b) {
            log.error(":>>> 批量创建黑名单错误业务异常1:{}", b);
            throw b;
        } catch (Exception e) {
            log.error(":>>> 批量创建黑名单错误业务异常2:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }
}
